// 题意：给定n(<=100)个城市和邻接矩阵给边。已经修好一些路，求最小生成树。
//
// 题解：裸prim，给妹子教学写的。
//
// 统计：94ms
// run: $exec < input
#include <iostream>
#include <queue>

struct edge { int from; int to; int cost; };

bool operator<(edge const & a, edge const & b)
{
	return a.cost > b.cost;
}

int const maxn = 101;
int map[maxn][maxn];
bool vis[maxn];
int n, q;

int prim()
{
	int min_span = 0;
	vis[1] = true;
	std::priority_queue<edge> pq;
	for (int i = 2; i <= n; i++) {
		edge tmp; tmp.from = 1; tmp.to = i; tmp.cost = map[1][i];
		pq.push(tmp);
	}
	for (int ti = 1; ti < n; ti++) {
		while (!pq.empty() && vis[pq.top().to]) pq.pop();
		edge now = pq.top();  pq.pop();
		vis[now.to] = true;
		min_span += now.cost;
		for (int i = 2; i <= n; i++) {
			if (i != now.to && !vis[i]) {
				edge tmp;
				tmp.from = now.to; tmp.to = i; tmp.cost = map[i][now.to];
				pq.push(tmp);
			}
		}
	}
	return min_span;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) std::cin >> map[i][j];

	std::cin >> q;
	for (int i = 0, x, y; i < q; i++) {
		std::cin >> x >> y;
		map[x][y] = map[y][x] = 0;
	}

	std::cout << prim() << '\n';
}

